﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.Windows.Media;

namespace OpenThesaurus
{
    public partial class History : PhoneApplicationPage
    {
        public History()
        {
            InitializeComponent();
            fillSearchHistory();
        }

        /// <summary>
        /// Adds the search term history items to the page.
        /// </summary>
        private void fillSearchHistory()
        {
            if (Search.searchTermHistory.Count > 0)
            { 
                HistoryStackPanel.Children.Clear();

                // Get the current Phone Accent Color
                Color currentAccentColorHex = (Color)Application.Current.Resources["PhoneAccentColor"];
                SolidColorBrush borderBrush = new SolidColorBrush(currentAccentColorHex);

                foreach (string searchHistoryItem in Search.searchTermHistory)
                {
                    // Create new control which will contain the search term
                    Button btnTerm = null;

                    // Create a new border control to enable rounded corners to the returned terms
                    Border borderTerm = null;

                    #region Create & configure the border control
                    // parent control which will contain the term button
                    borderTerm = new Border();
                    borderTerm.CornerRadius = new CornerRadius(25);
                    borderTerm.BorderBrush = borderBrush; // assign the Phone Accent Color to the term border
                    borderTerm.BorderThickness = new Thickness(3, 3, 3, 3);
                    borderTerm.Margin = new Thickness(0, 0, 0, 10); // margin between each single control in list
                    #endregion

                    #region Use a TextBlock control for text to support word wrap for long words (without it, long words are simply cut)
                    TextBlock txtTermContent = new TextBlock();
                    txtTermContent.Text = searchHistoryItem;
                    txtTermContent.TextAlignment = TextAlignment.Center;
                    txtTermContent.TextWrapping = TextWrapping.Wrap;
                    txtTermContent.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
                    txtTermContent.VerticalAlignment = System.Windows.VerticalAlignment.Stretch;
                    #endregion


                    #region Create & configure one button for each single term
                    btnTerm = new Button();
                    btnTerm.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
                    btnTerm.VerticalAlignment = System.Windows.VerticalAlignment.Stretch;
                    btnTerm.BorderThickness = new Thickness(0, 0, 0, 0); // remove button border - will be replaced by the rounded border of the parent control
                    btnTerm.Click += new RoutedEventHandler(OnTermButtonClicked);
                    #endregion

                    // assign the TextBlock control to the term button
                    btnTerm.Content = txtTermContent;

                    // put the button into the border control (necessary to have rounded corners)
                    borderTerm.Child = btnTerm;

                    HistoryStackPanel.Children.Add(borderTerm);
                }
            }
        }

        /// <summary>
        /// Event handler for the history term button's OnClick method.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnTermButtonClicked(object sender, RoutedEventArgs e)
        {
            Button clickedButton = sender as Button;

            // get the TextBlock from the Content property
            TextBlock txtTermContent = clickedButton.Content as TextBlock;

            string cleanedSearchTerm = Search.removeMetaDataFromSearchTerm(txtTermContent.Text);

            // apply the cleaned term as new input parameter for a search.
            // Since the Cortana speech integration is already there it gets reused to handle the new search.
            NavigationService.Navigate(new Uri(string.Format("/Search.xaml?voiceCommandName=ShowSynonyms&dictatedSearchTerms={0}", cleanedSearchTerm), UriKind.Relative));
        }

    }
}